/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package racesuitefx;

import DAO.RaceDAO;
import java.net.URL;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import racesuitefx.entities.Race;
import racesuitefx.entities.Starter;

/**
 * FXML Controller class
 *
 * @author Wolfgang
 */
public class FXMLAddRaceController implements Initializable {

    /**
     * Initializes the controller class.
     */
    RaceDAO raceDao;
    @FXML
    private TextField name;
    @FXML
    private TextField place;
    @FXML
    private DatePicker date;
    @FXML
    private TableView<Race> table;
 
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        raceDao = new RaceDAO();
        // create Table
        TableColumn name = new TableColumn("Name");
        name.setCellValueFactory(new PropertyValueFactory<Race, String>("name"));

        TableColumn place = new TableColumn("Ort");
        place.setCellValueFactory(new PropertyValueFactory<Race, String>("place"));

        TableColumn datum = new TableColumn("Datum");
        datum.setCellValueFactory(new PropertyValueFactory<Race, Date>("date"));
         table.setItems( FXCollections.observableArrayList(raceDao.findAll()));
        table.getColumns().addAll(name, place, datum);
        table.autosize();
       
    }    
    public void addRace(){
        Race r = new Race();
        r.setName(this.name.getText());
        r.setPlace(this.name.getText());
        LocalDate d = this.date.getValue();
        Instant instant = d.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
        r.setDate(Date.from(instant));
        raceDao.create(r);
        // refresh Table
        table.setItems( FXCollections.observableArrayList(raceDao.findAll()));
    }
}
